package com.xiaohutech.tm.home

import androidx.annotation.NonNull
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.xiaohutech.tm.home.databinding.HomeActivityBinding
import com.xiaohutech.tm.home.ui.fm.HomeFragment
import com.xiaohutech.tm.home.utils.BnvMediator
import com.yxh.lib_base.base.BaseActivity
import com.yxh.lib_base.ext.lazyNone
import com.yxh.lib_base.ext.viewbinding.binding

/**
 * @author yuanxh
 * @date :2024/2/4 21:13
 * @description:
 */
class HomeActivity:BaseActivity() {

    override val mBinding by binding<HomeActivityBinding>()

    private val mFragments by lazyNone {
        arrayListOf(
            HomeFragment(),
            HomeFragment()
        )
    }

    private var mBnvMediator: BnvMediator? = null

    override fun onCreated() {
        initViewPages()
        mBnvMediator = BnvMediator(mBinding.bnve, mBinding.viewPage)
        mBnvMediator?.attach()
    }

    private fun initViewPages() = with(mBinding) {
//        vpMain.offscreenPageLimit = mFragments.size - 1  //预加载数
//        vpMain.currentItem = defIndex
//        vpMain.adapter = mPageAdapter

        // ORIENTATION_HORIZONTAL：水平滑动（默认），ORIENTATION_VERTICAL：竖直滑动
        // ORIENTATION_HORIZONTAL：水平滑动（默认），ORIENTATION_VERTICAL：竖直滑动
        viewPage.orientation = ViewPager2.ORIENTATION_HORIZONTAL
        viewPage.isUserInputEnabled = false
        // 适配
        // 适配
        viewPage.adapter = object : FragmentStateAdapter(supportFragmentManager, lifecycle) {
            @NonNull
            override fun createFragment(position: Int): Fragment {
                return mFragments[position]
            }

            override fun getItemCount(): Int {
                return mFragments.size
            }
        }

    }
}